package com.ruoyi.project.system.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.DtQuery;
import com.ruoyi.project.system.domain.DtRisk;
import com.ruoyi.project.system.domain.vo.DtLabel;
import com.ruoyi.project.system.mapper.DtQueryMapper;
import com.ruoyi.project.system.mapper.DtRiskMapper;
import com.ruoyi.project.system.service.IDtRiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DtRiskServiceImpl implements IDtRiskService {
    @Autowired
    private DtRiskMapper dtRiskMapper;

    @Autowired
    private DtQueryMapper dtQueryMapper;

    @Override
    public JSONObject riskLabel(String type) {
        List<DtLabel> dtRisks = dtRiskMapper.queryRiskLabel(type);
        if (dtRisks == null || dtRisks.isEmpty()) {
            return null;
        } else {
            JSONObject ret = new JSONObject();
            for (DtLabel dl : dtRisks) {
                if (ret.containsKey(dl.getType())) {
                    ret.getJSONArray(dl.getType()).add(dl.getLabel());
                } else {
                    JSONArray array = new JSONArray();
                    array.add(dl.getLabel());
                    ret.put(dl.getType(), array);
                }
            }
            return ret;
        }
    }

    @Override
    public int pullBlackHandle(Integer queryId, String type, String label) {
        DtQuery dtQuery = dtQueryMapper.selectDtQueryByQueryId(queryId);
        if (dtQuery == null) {
            return 0;
        }
        DtRisk dtRisk = new DtRisk(dtQuery, type, label);
        Long userId = dtQueryMapper.selectUserIdByQueryId(queryId);
        dtRisk.setCreateBy(userId.toString());
        dtRiskMapper.pullBlackInsert(dtRisk);
        return dtRisk.getId();
    }

    @Override
    public DtRisk selectDtRiskById(Integer id) {
        return dtRiskMapper.selectDtRiskById(id);
    }

    @Override
    public List<DtRisk> selectDtRiskList(DtRisk dtRisk) {
        return dtRiskMapper.selectDtRiskList(dtRisk);
    }

    @Override
    public int insertDtRisk(DtRisk dtRisk) {
        return dtRiskMapper.insertDtRisk(dtRisk);
    }

    @Override
    public int updateDtRisk(DtRisk dtRisk) {
        dtRisk.setUpdateTime(DateUtils.getNowDate());
        return dtRiskMapper.updateDtRisk(dtRisk);
    }

    @Override
    public int deleteDtRiskByIds(Integer[] ids) {
        return dtRiskMapper.deleteDtRiskByIds(ids);
    }

    @Override
    public int deleteDtRiskById(Integer id) {
        return dtRiskMapper.deleteDtRiskById(id);
    }
}
